package com.pv.bfc.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Transactional
@Table(name = "FOLDER")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("FOLDER")
@DiscriminatorColumn(name = "DTYPE")
@NamedQueries( {
		@NamedQuery(name = "userFolders", query = "select a from Folder a where a.userName = ?"),
		@NamedQuery(name = "allFolders", query = "select a from Folder a") })
public class Folder implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	// @GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
	// "fold_seq_gen")
	// @SequenceGenerator(name = "fold_seq_gen", sequenceName = "FODLERID_SEQ")
	@Column(name = "folderid")
	private Integer folderid;

	@Column(name = "USERNAME")
	private String userName;

	@Column(name = "FOLDER_NAME")
	private String folderName;

	@Column(name = "DESCRIPTION")
	private String description;

	@Column(name = "CREATION_DATE")
	@Temporal(TemporalType.DATE)
	private Date creationDate = new Date();

	private String[] labels;

	// eagar fetching for web services and other remoting such as DWR
	// default is lasy loading

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private List<Document> documents = new ArrayList<Document>();
	
	public void addNewDocument(Document document) {
		document.setFolderId(folderid);
		documents.add(document);
	}

	public Folder() {
	}

	public Folder(String folderName) {
		this.folderName = folderName;
	}

	public String getFolderName() {
		return folderName;
	}

	public Integer getFolderid() {
		return folderid;
	}

	public void setFolderid(Integer folderid) {
		this.folderid = folderid;
	}

	public void setFolderName(String folderName) {
		this.folderName = folderName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public String[] getLabels() {
		return labels;
	}

	public void setLabels(String[] labels) {
		this.labels = labels;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public boolean getIsNew() {
		return getFolderid() == null;
	}
	
	/**
	 * @return  the pictures
	 * @uml.property  name="documents"
	 */
	public List<Document> getDocuments() {
		return documents;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = super.hashCode();
		result = PRIME * result
				+ ((creationDate == null) ? 0 : creationDate.hashCode());
		result = PRIME * result
				+ ((description == null) ? 0 : description.hashCode());
		result = PRIME * result
				+ ((folderid == null) ? 0 : folderid.hashCode());
		result = PRIME * result + Arrays.hashCode(labels);
		result = PRIME * result
				+ ((folderName == null) ? 0 : folderName.hashCode());
		result = PRIME * result
				+ ((userName == null) ? 0 : userName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Folder other = (Folder) obj;
		if (creationDate == null) {
			if (other.creationDate != null)
				return false;
		} else if (!creationDate.equals(other.creationDate))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (folderid == null) {
			if (other.folderid != null)
				return false;
		} else if (!folderid.equals(other.folderid))
			return false;
		if (!Arrays.equals(labels, other.labels))
			return false;
		if (folderName == null) {
			if (other.folderName != null)
				return false;
		} else if (!folderName.equals(other.folderName))
			return false;
		if (userName == null) {
			if (other.userName != null)
				return false;
		} else if (!userName.equals(other.userName))
			return false;
		return true;
	}
}
